package engine.fov;

import java.util.ListIterator;

/**
 * An iterator that behaves like C iterators
 * 
 * @author sdatta
 * @param <T>
 *            the type to iterate over
 */
class CLikeIterator<T> {

	private final boolean atBegin = false;
	private boolean atEnd = false;
	T curr;
	ListIterator<T> it;

	/**
	 * @param it
	 *            a ListIterator
	 */
	public CLikeIterator(ListIterator<T> it) {
		super();
		this.it = it;
		if (it.hasNext()) {
			curr = it.next();
		} else {
			curr = null;
			atEnd = true;
		}
		// checkPrevNext();
	}

	// private final void checkPrevNext()
	// {
	// // if(it.hasNext())
	// // atEnd=false;
	// // else
	// // atEnd=true;
	//		
	// if(it.hasPrevious())
	// atBegin=false;
	// else
	// atBegin=true;
	// }

	protected final T getCurrent() {
		return curr;
	}

	protected void gotoNext() {
		if (it.hasNext()) {
			curr = it.next();
		} else {
			atEnd = true;
			curr = null;
		}
		// checkPrevNext();
	}

	protected void gotoPrevious() {
		if (it.hasPrevious()) {
			curr = it.previous();
		}
		// else
		// {
		// curr=null;
		// }
		// checkPrevNext();
	}

	protected void insertBeforeCurrent(T t) {
		it.previous();
		it.add(t);
		// checkPrevNext();
	}

	protected boolean isAtBeginning() {
		return atBegin;
	}

	protected boolean isAtEnd() {
		return atEnd;
	}

	protected void removeCurrent() {
		it.remove();
		gotoNext();
	}
}
